import java.io.*;
import java.util.*;

/**
 * @version 1.12 2007-06-22
 * @author Cay Horstmann
 */
public class TextFileTest
{
   public static void main(String[] args)
   {
	   Student[] staff = new Student[3];

      staff[0] = new Student(1,"A",18,1);
      staff[1] = new Student(2,"B",19,2);
      staff[2] = new Student(3,"C",20,3);

      try
      {
         PrintWriter out = new PrintWriter("student.dat");
         writeData(staff, out);
         out.close();

        
         Scanner in = new Scanner(new FileReader("student.dat"));
         Student[] newStaff = readData(in);
         in.close();

         for (Student e : newStaff)
            System.out.println(e);
      }
      catch (IOException exception)
      {
         exception.printStackTrace();
      }
   }

   /**
    * Writes all employees in an array to a print writer
    * @param employees an array of employees
    * @param out a print writer
    */
   private static void writeData(Student[] student, PrintWriter out) throws IOException
   {
      // write number of employees
      out.println(student.length);

      for (Student e : student)
         e.writeData(out);
   }

   /**
    * Reads an array of employees from a scanner
    * @param in the scanner
    * @return the array of employees
    */
   private static Student[] readData(Scanner in)
   {
      // retrieve the array size
      int n = in.nextInt();
      in.nextLine(); // consume newline

      Student[] student = new Student[n];
      for (int i = 0; i < n; i++)
      {
    	  student[i] = new Student();
    	  student[i].readData(in);
      }
	return student;
   }
}

class Student
{
   public Student(int id, String name, int age, double grade)
   {
		this.id = id;
		this.setName(name);
		this.setAge(age);
		this.setGrade(grade);
	}

	public Student() {
	
}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		if (name.length()>10){
			throw new IllegalArgumentException("name's length should <=10 "+name.length());
		}
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		if (age<=0){
			throw new IllegalArgumentException("age should >0 "+age);
		}
		this.age = age;
	}
	public double getGrade() {
		return grade;
	}
	public void setGrade(double grade) {
		if (grade<0 || grade >100){
			throw new IllegalArgumentException("grade should be in [0,100] "+grade);
		}
		this.grade = grade;
		
	}
	@Override
	public String toString() {
	return "Student [id=" + id + ", name=" + name + ", age=" + age + ", grade=" + grade + "]";
}



   /**
    * Writes employee data to a print writer
    * @param out the print writer
    */
   public void writeData(PrintWriter out)
   {
      //GregorianCalendar calendar = new GregorianCalendar();
      out.println(id + "|" + name + "|" + age + "|" + grade);
   }

   /**
    * Reads employee data from a buffered reader
    * @param in the scanner
    */
   public void readData(Scanner in)
   {
      String line = in.nextLine();
      String[] tokens = line.split("\\|");
      id = Integer.parseInt(tokens[0]);
      name = tokens[1];
      age = Integer.parseInt(tokens[2]);
      grade = Double.parseDouble(tokens[3]);
      //GregorianCalendar calendar = new GregorianCalendar(y, m - 1, d);
   }

	private int id;
	private String name;
	private int age;
	private double grade;
}